<?php
class CategoriesController extends AppController {

    public $helpers = array('Form','Pagination','Rating');

    public function admin_index() {
        $this->Category->recursive = 1;
        $this->set('categories', $this->paginate());
    }

    public function admin_add() {
        if (!empty($this->data)) {
            $this->Category->create();
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash(__('The Category has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Category could not be saved. Please, try again.',true));
            }
        }
        $this->set('articles',$this->Category->Article->find('list'));
    }

    public function admin_edit($id = null) {
        if (!$this->Category->findById($id)) {
            $this->Session->setFlash(__('Invalid category',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if (!empty($this->data)) {
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash(__('The Category has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Category could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->Category->read(null, $id);
        }
        $this->set('articles',$this->Category->Article->find('list'));
    }

    public function admin_delete($id = null) {
        if (!$this->Category->findById($id)) {
            $this->Session->setFlash(__('Invalid category',true));
        }
        if ($this->Category->del($id)) {
            $this->Session->setFlash(__('Category deleted',true));
        } else {
            $this->Session->setFlash(__('The Categories could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function admin_delete_categories() {
        if ($this->Category->deleteAll(array('Category.id'=>$this->data['CategoriesDelete']))) {
            $this->Session->setFlash(__('Categories deleted',true));
        } else {
            $this->Session->setFlash(__('The Categories could not be deleted. Please, try again.',true));
        }

        $this->redirect(array('action'=>'index'), null, true);
    }

    public function index() {
        $this->Category->recursive = 2;
        $this->set('categories',$this->paginate());
    }

    public function view($seo = null) {
        if(!$this->Category->find(array('seo_link'=>$seo))) {
            $this->Session->setFlash(__('Invalid category',true));
            $this->redirect(array('action'=>'index'), null, true);
        } else {
            $this->Category->recursive = 2;
            $this->paginate = array('conditions'=>'Category.seo_link="'.$seo.'"','limit'=>5);
            $category = $this->paginate();
            $url = $seo;
            $this->set(compact('category','url'));
        }
    }

    public function sidebox() {
        return $this->Category->findAll();
    }
}
?>